# 普通用户查询自己员工信息部分
from tkinter import messagebox

import pymysql
from tkinter import *
import tkinter as tk

'''
def connect_to_database():
    # 连接数据库
    connect = pymysql.connect(host="localhost", user="root", password="hsb010811", database="618")
    return connect
'''


def display_employee_info(employee_id):
    connect = pymysql.connect(host="localhost", user="root", password="hsb010811", database="618")
    db_connection = connect
    cursor = db_connection.cursor()
    # *******************
    # 创建视图
    # cursor.execute("CREATE VIEW employee_info_view AS SELECT * FROM 员工表 WHERE 员工编号 = %s", (employee_id,))
    # *******************
    # 查询视图数据
    cursor.execute("SELECT * FROM 员工表 WHERE 员工编号 = %s", (employee_id,))
    employee_info = cursor.fetchall()

    db_connection.close()
    return employee_info


def on_button_click():
    yg_id = str(employee_id_entry.get())
    employee_info = display_employee_info(yg_id)
    if yg_id != ygbh:
        messagebox.showinfo("提示", "请输入与登录账号一致的员工编号！")
    else:
        messagebox.showinfo("提示", "验证成功！")
        for info in employee_info:
            result_text.insert(END,
                               f"员工编号: {info[0]}\n员工姓名: {info[1]}\n员工密码: {info[2]}\n员工电话: {info[3]}\n员工工资: {info[4]}\n备注: {info[5]}\n\n")


def ygxx_page():
    global yg_id, result_text, employee_id_entry
    root = Tk()
    root.title("员工个人信息查询")

    Label(root, text="我的员工编号:").pack()
    employee_id_entry = Entry(root)
    employee_id_entry.pack()

    # yg_id = str(employee_id_entry.get())

    query_button = Button(root, text="查询我的信息", command=on_button_click)
    query_button.pack()

    result_text = Text(root, height=10, width=50)
    result_text.pack()

    root.mainloop()


# 普通员工界面
def ordinary_page():
    global root_ordinary
    root_ordinary = tk.Tk()
    root_ordinary.title("普通员工")
    root_ordinary.geometry("500x500")
    root_ordinary.resizable(False, False)

    # 设置窗口在屏幕中间弹出
    root_ordinary.withdraw()
    root_ordinary.update_idletasks()
    x = (root_ordinary.winfo_screenwidth() - root_ordinary.winfo_reqwidth()) // 2
    y = (root_ordinary.winfo_screenheight() - root_ordinary.winfo_reqheight()) // 2
    root_ordinary.geometry("+%d+%d" % (x, y))
    root_ordinary.deiconify()

    tk.Label(root_ordinary, text="欢迎进入查询界面", font=("黑体", 20)).pack(pady=10)
    button1 = tk.Button(root_ordinary, text="查询员工信息", command=ygxx_page).pack(pady=10)
    button2 = tk.Button(root_ordinary, text="查询商品信息", command=on_button_click).pack(pady=10)
    button3 = tk.Button(root_ordinary, text="查询采购信息", command=on_button_click).pack(pady=10)
    button4 = tk.Button(root_ordinary, text="查询采购明细信息", command=on_button_click).pack(pady=10)
    button5 = tk.Button(root_ordinary, text="退出", command=login_page).pack(pady=10)


# 登录
def submit(input_id, input_password, input_level):
    connect = pymysql.connect(host="localhost", user="root", password="hsb010811", database="618")
    cursor = connect.cursor()

    sql = "SELECT * FROM 员工表"
    # *******************
    global ygbh
    # *******************
    try:
        cursor.execute(sql)
        employees = cursor.fetchall()

        input_list = [str(input_id), str(input_password), str(input_level)]  # 用户输入的列表
        employee_data_list = []
        # *******************
        ygbh = str(input_id)
        # *******************
        for employee in employees:
            employee_data_list = ([employee[0], employee[2], employee[3]])
            # employee_list = list(employee)  # 员工表输出的列表
            if input_list == employee_data_list:
                if input_list[2] == "20":  # 判断是否为管理员
                    messagebox.showinfo("登录成功", "管理员登录成功")
                    # to_manager_page()
                else:
                    messagebox.showinfo("登录成功", "普通用户登录成功")
                    ordinary_page()
                return

        messagebox.showerror("登录失败", "用户名或密码错误")

    except Exception as e:
        print("登录出错:", e)
        messagebox.showerror("错误", "登录出错")

    finally:
        connect.close()


# 登录界面
def login_page():
    global window_function, id_entry, password_entry, level_entry
    window_function = tk.Tk()
    window_function.title("用户登录")
    window_function.geometry('400x200')

    tk.Label(window_function, text="编号：").grid(row=0, column=0, padx=20, pady=10)
    id_entry = tk.Entry(window_function)
    id_entry.grid(row=0, column=1)

    tk.Label(window_function, text="密码：").grid(row=1, column=0, padx=20, pady=10)
    password_entry = tk.Entry(window_function, show="*")
    password_entry.grid(row=1, column=1)

    tk.Label(window_function, text="级别：").grid(row=2, column=0, padx=20, pady=10)
    level_entry = tk.Entry(window_function)
    level_entry.grid(row=2, column=1)

    login_button = tk.Button(window_function, text="登录",
                             command=lambda: submit(id_entry.get(), password_entry.get(), level_entry.get()))
    login_button.place(relx=0.5, rely=0.7)

    window_function.mainloop()


login_page()